﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RFIDStockManagement.DAL.Reports;
using RFIDStockManagement.Model.Public;
using RFIDStockManagement.Common.Public;

namespace RFIDStockManagement.DAL.Reports
{
    public class ReportStockCheckDetail : DBReportBase
    {
        private int checkPId = 0;
        private int stockId = 0;
        private int reportType = 0; //0是盘盈,1是盘亏


        private const string SQL_MORE_CHECK = @"
--盘赢
Select  t3.FName as FitemName,t3.FNumber as FitemNumber,t1.FRfid as FRFID,
t4.FName as FStock,t5.FName as FStockPlace,t6.FName as FUnit,getDate() as FDate
From RFID_Stock_Check_Detail t1
Inner Join rfid_Item t3 on t1.FItemId = t3.Fid
Inner Join rfid_Stock t4 on t4.Fid = t1.FStockId
Left Join rfid_StockPlace t5 on t5.Fid = t1.FStockPlaceId
Inner Join rfid_Measure t6 on t6.Fid = t1.FUnitID
Where t1.Fid ={0} And (Not Exists(select 1 from rfid_Item_Detail tt1 where tt1.FRFID = t1.FRFID And  tt1.FStatus<=1)) ";

        private const string SQL_LESS_CHECK = @"
--盘亏
Select t3.FName as FitemName,t3.FNumber as FitemNumber,t1.FRfid as FRFID,
t4.FName as FStock,t5.FName as FStockPlace,t6.FName as FUnit,getDate() as FDate
From rfid_Item_Detail t1
Inner Join rfid_Item t3 on t1.FItemId = t3.Fid
Inner Join rfid_Stock t4 on t4.Fid = t1.FLastInStockPlaceId
Left Join rfid_StockPlace t5 on t5.Fid = t1.FLastInStockID
Inner Join rfid_Measure t6 on t6.Fid = t1.FUnitID
Where t1.FStatus <=1 And Not Exists(select 1 from RFID_Stock_Check_Detail tt1 where tt1.FRFID = t1.FRFID And tt1.Fid ={0})";

        public ReportStockCheckDetail()
        {
            this.ReportCaption = "盘盈盘亏报告表";
            this.ReportKey = "detail_stock_check";
            InitColuns();
        }

        private void InitColuns()
        {
            ReportColumn newCol;
     
            newCol = AddColumn("FitemName", "物料名称");
            newCol.Index = 1;

            newCol = AddColumn("FitemNumber", "物料代码");
            newCol.Index = 2;

            newCol = AddColumn("FRFID", "RFID");
            newCol.Index = 3;

            newCol = AddColumn("FStock", "所处仓库");
            newCol.Index = 4;

            newCol = AddColumn("FStockPlace", "所处仓位");
            newCol.Index = 5;

            newCol = AddColumn("FUnit", "单位名称");
            newCol.Index = 6;

            newCol = AddColumn("FDate", "最后入库时间");
            newCol.Index = 7;
            newCol.ColumnType = RFIDStockManagement.Common.Public.Enums.EnumDataTypes.TDateTime;
            newCol.FormateString = PublicConst.LongTimeFormater;

        }

        public override System.Data.DataTable GetReportData()
        {
            string sql;
            string filter = GetFilterString();
            string testStr = GetFilterData("reportType");
            
            if (testStr == string.Empty || testStr == "0") //盘盈
            {
                sql = SQL_MORE_CHECK;
                Columns[6].Visible = false;
            }
            else
            {
                sql = SQL_LESS_CHECK;
                Columns[6].Visible = true;
            }


            if (checkPId <= 0)
            {
                return new System.Data.DataTable();
            }

            sql = string.Format(sql, checkPId);
            sql += filter;

            return GetData(sql);


        }


        private string GetFilterString()
        {
            string retString = "";
            string testStr = string.Empty;

            if (Filters == null)
            {
                checkPId = 0;
                stockId = 0;
                return string.Empty;
            }

            testStr = GetFilterData("stockcheckId");

            if (!int.TryParse(testStr, out checkPId))
            {
                checkPId = 0;
                return string.Empty;
            }

            testStr = GetFilterData("stockId");

            if (!int.TryParse(testStr, out stockId))
            {
                stockId = 0;

            }
            else
            {
                if (stockId != 0)
                {
                    retString += " And t4.FId = " + stockId.ToString();
                }
            }

            testStr = GetFilterData("FItemStart");

            if (!string.IsNullOrEmpty(testStr))
            {
                retString += " And t3.FName >='" + testStr + "' ";
            }

            testStr = GetFilterData("FItemEnd");

            if (!string.IsNullOrEmpty(testStr))
            {
                retString += " And t3.FName <='" + testStr + "' ";
            }

            return retString;

        }

    }
}
